﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;

namespace Asxsyd92Core.Utils
{
    /*===========================================================================
 // Copyright (C) 2016  爱上歆随懿恫
 // 作者：陈良富 asxsyd92@foxmail.com qq：1316227882
 // 功能描述：添加Tools类

 // 更新日期        更新人      更新原因/内容
 //
 ===========================================================================*/
    public class Tools
    {

        #region+获取浏览器端操作系统名称
        /// <summary>
        /// 获取浏览器端操作系统名称
        /// </summary>
        /// <returns></returns>
        //public static string GetOSName()
        //{
        //    string osVersion = System.Web.HttpContext.Current.Request.Browser.Platform;
        //    string userAgent = System.Web.HttpContext.Current.Request.UserAgent;

        //    if (userAgent.Contains("NT 6.3"))
        //    {
        //        osVersion = "Windows8.1";
        //    }
        //    else if (userAgent.Contains("NT 6.2"))
        //    {
        //        osVersion = "Windows8";
        //    }
        //    else if (userAgent.Contains("NT 6.1"))
        //    {
        //        osVersion = "Windows7";
        //    }
        //    else if (userAgent.Contains("NT 6.0"))
        //    {
        //        osVersion = "WindowsVista";
        //    }
        //    else if (userAgent.Contains("NT 5.2"))
        //    {
        //        osVersion = "WindowsServer2003";
        //    }
        //    else if (userAgent.Contains("NT 5.1"))
        //    {
        //        osVersion = "WindowsXP";
        //    }
        //    else if (userAgent.Contains("NT 5"))
        //    {
        //        osVersion = "Windows2000";
        //    }
        //    else if (userAgent.Contains("NT 4"))
        //    {
        //        osVersion = "WindowsNT4.0";
        //    }
        //    else if (userAgent.Contains("Me"))
        //    {
        //        osVersion = "WindowsMe";
        //    }
        //    else if (userAgent.Contains("98"))
        //    {
        //        osVersion = "Windows98";
        //    }
        //    else if (userAgent.Contains("95"))
        //    {
        //        osVersion = "Windows95";
        //    }
        //    else if (userAgent.Contains("Mac"))
        //    {
        //        osVersion = "Mac";
        //    }
        //    else if (userAgent.Contains("Unix"))
        //    {
        //        osVersion = "UNIX";
        //    }
        //    else if (userAgent.Contains("Linux"))
        //    {
        //        osVersion = "Linux";
        //    }
        //    else if (userAgent.Contains("SunOS"))
        //    {
        //        osVersion = "SunOS";
        //    }
        //    return osVersion;
        //}
        #endregion+得到用户浏览器类型
        #region+
        /// <summary>
        /// 得到用户浏览器类型
        /// </summary>
        /// <returns></returns>
        //public static string GetBrowse()
        //{
        //    return System.Web.HttpContext.Current.Request.Browser.Type;
        //}
        #endregion
        #region+得到加密后的密码字符串
        /// <summary>
        /// 得到加密后的密码字符串
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string EncryptionPassword(string password)
        {
            if (string.IsNullOrEmpty(password))
            {
                return "";
            }

            return MD5System(MD5System(password)); //hash.SHA512Encrypt(hash.SHA512Encrypt(password.Trim()));
        }
        #endregion+得到一个用户加密后的密码
        /// <summary>
        /// 得到一个用户加密后的密码
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string GetUserEncryptionPassword(string userID, string password)
        {
            return string.IsNullOrEmpty(password) || string.IsNullOrEmpty(userID) ? "" : EncryptionPassword(userID.Trim().ToLower() + password.Trim());
        }
        #region+.net系统的MD5加密方法
        /// <summary>
        /// .net系统的MD5加密方法
        /// </summary>
        /// <param name="strIN"></param>
        /// <returns></returns>
        public static string MD5System(string strIN)
        {
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(strIN))).Replace("-", null);
            byte[] byteResult = md5.ComputeHash(Encoding.UTF8.GetBytes(strIN));//获取加密后的序列
            string strResult = BitConverter.ToString(byteResult);//转换为十六进制
            string realResult = strResult.Replace("-", null);
            return realResult;
            //  return string.IsNullOrEmpty(strIN) ? "" : "";//System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strIN, "MD5");
        }
        #endregion







        /// <summary>  
        /// 过滤特殊字符  
        /// </summary>  
        /// <param name="s"></param>  
        /// <returns></returns>  
        public static string String2Json(String s)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s.ToCharArray()[i];
                switch (c)
                {
                    case '\"':
                        sb.Append("\\\""); break;
                    case '\\':
                        sb.Append("\\\\"); break;
                    case '/':
                        sb.Append("\\/"); break;
                    case '\b':
                        sb.Append("\\b"); break;
                    case '\f':
                        sb.Append("\\f"); break;
                    case '\n':
                        sb.Append("\\n"); break;
                    case '\r':
                        sb.Append("\\r"); break;
                    case '\t':
                        sb.Append("\\t"); break;
                    case '\v':
                        sb.Append("\\v"); break;
                    case '\0':
                        sb.Append("\\0"); break;
                    default:
                        sb.Append(c); break;
                }
            }
            return sb.ToString();
        }

        /// <summary>  
        /// 格式化字符型、日期型、布尔型  
        /// </summary>  
        /// <param name="str"></param>  
        /// <param name="type"></param>  
        /// <returns></returns>  
        private static string StringFormat(string str, Type type)
        {
            if (type != typeof(string) && string.IsNullOrEmpty(str))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(string))
            {
                str = String2Json(str);
                str = "\"" + str + "\"";
            }
            else if (type == typeof(DateTime))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(bool))
            {
                str = str.ToLower();
            }
            else if (type == typeof(byte[]))
            {
                str = "\"" + str + "\"";
            }
            else if (type == typeof(Guid))
            {
                str = "\"" + str + "\"";
            }
            return str;
        }
        public static void createFile(string str)
        {
            try {
                
                if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory+"log.txt"))
                {
                    FileStream fs1 = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "log.txt", FileMode.Create, FileAccess.Write);//创建写入文件 
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine(str + "【" + System.DateTime.Now.ToString() + "】");//开始写入值
                    sw.Close();
                    fs1.Close();
                }
                else
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + "log.txt";//文件的路径，保证文件存在。
                    FileStream fs = new FileStream(path, FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(str + "【" + System.DateTime.Now.ToString() + "】");
                    sw.Close();
                    fs.Close();
                }
            } catch  { }
         
        }
        /// <summary>
        /// 生成4个短短链接
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string[] ShortUrl(string url)
        {
            //可以自定义生成MD5加密字符传前的混合KEY
            string key = "asxsyd92";
            //要使用生成URL的字符
            string[] chars = new string[]{
            "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,
            "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,
            "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,
            "y" , "z" , "0" , "1" , "2", "3" ,"4","5","6" ,
            "7","8","9","A","B","C","D", "E" , "F" , "G" ,
            "H" , "I" , "J" , "K" , "L" , "M" , "N" , "O" ,
            "P" , "Q" , "R" , "S" , "T" ,"U" , "V" , "W" ,
            "X" , "Y" , "Z"
            };
            //对传入网址进行MD5加密
            string hex = EncryptionPassword(key + url);// FormsAuthentication.HashPasswordForStoringInConfigFile(key + url,"md5");
            string[] resUrl = new string[4];
            for (int i = 0; i < 4; i++)
            {
                //把加密字符按照8位一组16进制与0x3FFFFFFF进行位与运算
                int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
                string outChars = string.Empty;
                for (int j = 0; j < 6; j++)
                {
                    //把得到的值与0x0000003D进行位与运算，取得字符数组chars索引
                    int index = 0x0000003D & hexint;
                    //把取得的字符相加
                    outChars += chars[index];
                    //每次循环按位右移5位
                    hexint = hexint >> 5;
                }
                //把字符串存入对应索引的输出数组
                resUrl[i] = outChars;
            }
            return resUrl;
        }

    }
}
